<template>
  <el-container class="login-container">
  <el-header>
    <el-button style="background:transparent;border:0;color:#fff;font-size:18px" @click="returnHome" icon="el-icon-back" size='mini'>返回首页</el-button>
      <!-- <div @click="returnHome"><h1 style="color:green"></h1></div> -->
      <div class="navTop">
        <router-link class="recon" to="/tRegister"><strong><span style="color:#ffbb02">教师注册</span></strong></router-link>
        <router-link class="recon" to="/sRegister"><strong><span style="color:#ffbb02">学校注册</span></strong></router-link>
        <router-link class="recon" to="/contact"><strong><span style="color:#ffbb02">忘记密码</span></strong></router-link>
      </div>
  </el-header>
  <el-main>
    <el-form ref="form" :inline="true" :model="form" :rules="rules" class="login-form">
      <h3>请登录</h3>
      <el-form-item prop="username">
        <el-input style="width:320px" prefix-icon="el-icon-user-solid" v-model="form.username" placeholder="账号">
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input style="width:320px" prefix-icon="el-icon-lock" v-model="form.password" type="password" placeholder="密码">
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input style="width:108px;" prefix-icon="el-icon-warning-outline" v-model="form.code" placeholder="验证码"></el-input>
      </el-form-item>
     <el-form-item>
         <div @click="refresCode" ref="canvas" class="login-code-img" id="canvasBox">
         <canvas class="show-captcha" id="canvas" style="width: 100%; height: 100%;"></canvas>
         </div>
      </el-form-item>
      <el-form-item>
    <el-radio-group v-model="form.role">
      <el-radio label="1">教师</el-radio>
      <el-radio label="2">学校</el-radio>
      <el-radio label="3">管理员</el-radio>
    </el-radio-group>
  </el-form-item>
  <el-form-item class="btn">
    <el-button type="primary" @click="loginHandler">登录</el-button>
    <el-button type="warn" @click="resetForm">重置</el-button>
  </el-form-item>
    </el-form>
  </el-main>
</el-container>
</template>

<script>
import drawPic from '@/utils/verificationCode'
import { getTeacher, getSchool, getManager } from '../api'
export default {
  data () {
    return {
      form: {
        username: '',
        password: '',
        role: '1',
        code: ''
      },
      verificationCode: '',
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 16, message: '长度在 3 到 9 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 16, message: '长度在 3 到 9 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 4, message: '长度在4个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 返回首页
    returnHome () {
      this.$router.push('/')
    },
    loginHandler () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (!(this.verificationCode.toLowerCase() === this.form.code.toLowerCase())) {
            return this.$message.error('验证码错误')
          }
          // console.log(this.form)
          if (this.form.role === '1') {
            this.sendRequest(getTeacher)
          } else if (this.form.role === '2') {
            this.sendRequest(getSchool)
          } else if (this.form.role === '3') {
            this.sendRequest(getManager)
          }
        }
      })
    },
    resetForm () {
      this.$refs.form.resetFields()
    },
    // 生成验证码
    makeCode () {
      drawPic.VerificationCodeErrCount = 0
      drawPic.randomCode = drawPic()
      this.verificationCode = drawPic.randomCode
      return drawPic.randomCode
    },
    // 刷新验证码
    refresCode () {
      this.makeCode()
    },
    async sendRequest (method) {
      const aa = await method(this.form)
      // console.log(this.from)
      method(this.form).then(({ data }) => {
        // console.log(data)
        if (data.status === 200) {
          this.$message.success(data.message)
          window.localStorage.setItem('token', data.token)
          window.localStorage.setItem('id', data.id)
          window.localStorage.setItem('imageSrc', data.imageSrc)
          this.$store.commit('updateRole', this.form.role)
          this.$store.commit('setMenu', data.menu)
          this.$store.commit('addMenu', this.$router)
          // this.$store.commit('saveImage', data.imageSrc)
          this.$store.commit('getUserInfo', data.result)
          // console.log(data.result)
          // console.log(data.imageSrc)
          this.$router.push('/home')
        } else {
          this.$message.error(data.message)
        }
      })
    }
  },
  mounted () {
    this.makeCode()
  }

}
</script>
<style lang='less' scoped>
.login-container{
  height: 100%;
  background-image: url('../assets/bgg.png');
  background-size: 100% 100%;
  .el-header{
    display: flex;
    padding: 0 20px;
    line-height: 60px;
    justify-content: space-between;
    background:#022ca5;
    opacity: 0.6;
    .recon{
      margin-right: 30px;
      text-decoration: none;
      color: black;
    }
  }
  .el-main{
    // background: pink;
    .login-form{
      width: 400px;
      border: 1px solid #eaeaea;
      margin: 80px auto;
      padding: 35px 35px 15px 35px;
      border-radius: 8px;
      box-shadow: 0 0 25px #cac6c6;
      background: #fff;
      opacity: 0.9;
      box-sizing: border-box;
      .btn{
        text-align: center;
      }
    }
    .login-code-img{
      width:88px;
      height:40px;
    }
  }
  .navTop{
    span:hover{color:violet;}
  }

}
</style>
